'use strict';

/**
 * 建筑管理Service
 */
app.service('AreaService',
    ['$http', 'Upload', '$filter',
        function ($http, Upload, $filter) {
            this.getGirdOptions = function () {
                var _gridWidth = 877;
                var _columnWidth = _gridWidth / 7;
                return {
                    // height: 430,
                    url: ISS.getPath("/arch/archList"),
                    dataType: 'json',
                    colModel: [
                        {display: 'area.name', name: 'name', width: _columnWidth},
                        {
                            display: 'area.parentName',
                            name: 'parentName',
                            width: _columnWidth,
                            process: function (value) {
                                return value == null ? "---" : value;
                            }
                        },
                        {display: 'area.longitude', name: 'longitude', width: _columnWidth * 0.7},
                        {display: 'area.latitude', name: 'latitude', width: _columnWidth * 0.7},
                        {
                            display: 'common.createTime',
                            name: 'createTime',
                            width: _columnWidth * 1.4,
                            process: function (data) {
                                return moment(data).format('YYYY-MM-DD HH:mm:ss');
                            }
                        },
                        {
                            display: 'common.operation', name: 'id', width: _columnWidth, process: function (id) {
                            var html = [];
                            html.push('<button iss-permission="B_ARCH_UPDATE" class="btn m-r-xs btn-xs btn-primary" ng-click="vm.updateArea(' + id + ')">');
                            html.push('  <i class="fa fa-pencil"></i>');
                            html.push('  <span translate="common.edit"></span>');
                            html.push('</button>');
                            html.push('<button iss-permission="B_ARCH_DELETE" class="btn m-r-xs btn-xs btn-danger" ng-click="vm.delArea(' + id + ')">');
                            html.push('  <i class="fa fa-trash-o"></i>');
                            html.push('  <span translate="common.del"></span>');
                            html.push('</button>');
                            return html.join('');
                        }
                        }
                    ],
                    usepager: true,
                    rp: 10,
                    showcheckbox: true
                };
            }
            this.getArea = function (id) {
                return $http.post(
                    ISS.getPath('/arch/get/' + id)
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.getNextNumber = function () {
                return $http.post(
                    ISS.getPath('/arch/getNextNumber')
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.addArea = function (area) {
                area.number = 'AD' + area.number;
                delete area.areaType;
                return $http.post(
                    ISS.getPath('/arch/add'),
                    area,
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.updateArea = function (area) {
                delete area.areaType;
                delete area.createTime;
                return $http.post(
                    ISS.getPath('/arch/update'),
                    area,
                    {contentType: 'json'}
                ).then(function (resp) {
                    return resp.data;
                });
            }
            this.deleteArea = function (id) {
                return $http.post(
                    ISS.getPath('/arch/del/' + id)
                ).then(function (resp) {
                    return resp.data;
                });
            }
        }]);